package com.shangqing.system.service.impl;

import com.shangqing.system.entity.CityGrade;
import com.shangqing.system.entity.Earthquake;
import com.shangqing.system.entity.EerthNumber;
import com.shangqing.system.entity.SysCity;
import com.shangqing.system.mapper.SysEarthquakeMapper;
import com.shangqing.system.service.ISysEarthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.text.NumberFormat;

@Service("ISysEarthService")
public class SysEarthServiceImpl implements ISysEarthService {
	@Autowired
	SysEarthquakeMapper sysEarthquakeMapper;


	/**
	 * 查找所有地震信息
	 *
	 * @return
	 */

	@Override
	public List<Earthquake> selectearthinfo() {
		List<Earthquake> earthquakeslist = new ArrayList<>();
		List<Earthquake> earthquakes = sysEarthquakeMapper.earthinfo();
		for (Earthquake eee : earthquakes
		) {
			SysCity sysCity = sysEarthquakeMapper.selectcity(eee.getParentsource());
			sysCity.setSysCity(City(sysCity));
			eee.setSysCity(sysCity);
			eee.setEarthGrade(sysEarthquakeMapper.selectgrade(eee.getDisasterGrade()));
			earthquakeslist.add(eee);
		}
		return earthquakeslist;
	}

	/**
	 * 循环
	 *
	 * @return
	 */
	public SysCity City(SysCity sysCity) {
		if (sysCity.getParentId() == 0) {
			return null;
		} else {
			SysCity sysCity1 = sysEarthquakeMapper.selectcity(sysCity.getParentId());
			SysCity sysCity2 = new SysCity();
			sysCity2.setId(sysCity1.getId());
			sysCity2.setCityName(sysCity1.getCityName());
			sysCity2.setSysCity(City(sysCity1));
			return sysCity2;
		}

	}

	/**
	 * 查询每个城市地震所占百分比
	 *
	 * @return
	 */

	@Override
	public List<EerthNumber> selectearthnum() {
		NumberFormat numberFormat = NumberFormat.getInstance();
		numberFormat.setMaximumFractionDigits(0);
		List<EerthNumber> erthNumbers = sysEarthquakeMapper.selectnum();
		Float a = (float) sysEarthquakeMapper.selectmax();
		System.out.println(a);
		List<EerthNumber> list = new ArrayList();
		for (EerthNumber e : erthNumbers) {
			if (a != null && erthNumbers != null) {
				String city = e.getCityname();
				float num = e.getNum();
				String sum = numberFormat.format(num / a * 100);
//				System.out.println(sum);
				EerthNumber eerthNumber = new EerthNumber();
				eerthNumber.setCityname(city);
				eerthNumber.setNum(Float.parseFloat(sum));
				list.add(eerthNumber);
			}
		}
		return list;
	}

	/**
	 * 统计每个城市不同震别的次数
	 *
	 * @return
	 */
	@Override
	public List<CityGrade> selectcountgrade(int year) {
		List<CityGrade> cityGrades = sysEarthquakeMapper.selectcitygrading(year);
		List<CityGrade> list = new ArrayList<>();
		for (CityGrade city : cityGrades
		) {
			String cityname = city.getCityname();
			String grade = city.getGrading();
			int count = city.getCount();
			CityGrade cityGrade = new CityGrade();
			cityGrade.setCityname(cityname);
			cityGrade.setGrading(grade);
			cityGrade.setCount(count);
			list.add(cityGrade);
		}
		return list;
	}

	/**
	 * 查询人口损失
	 *
	 * @return
	 */
	@Override
	public List<Map> selectpopulation(int year) {
		List<Map> maps = sysEarthquakeMapper.selectpeople(year);
		return maps;
	}

	/**
	 * 查询房屋损失
	 *
	 * @return
	 */
	@Override
	public List<Map> selecthomeloss(int year) {
		List<Map> maps = sysEarthquakeMapper.selecthome(year);
		return maps;
	}

	/**
	 * 查询经济损失
	 *
	 * @return
	 */
	@Override
	public List<Map> selectmoneyloss(int year) {
		List<Map> maps = sysEarthquakeMapper.selectmoney(year);
		return maps;
	}

	/**
	 * 年度各震级发生次数
	 *
	 * @param year
	 * @return
	 */
	@Override
	public List<Map> selecteveryyear(int year) {
		List<Map> maps = sysEarthquakeMapper.counteveryyear(year);
		return maps;
	}

	/**
	 * 查询近五年每年发生的地震次数
	 *
	 * @return
	 */
	@Override
	public List<Map> selectfiveyear() {
		List<Map> maps = sysEarthquakeMapper.selfiveyear();
		return maps;
	}

	/**
	 * 根据id查询死亡人数和房屋倒塌数
	 *
	 * @param id
	 * @return
	 */
	@Override
	public List<Map> selectdiehome(int id) {
		List<Map> maps = sysEarthquakeMapper.seldiehome(id);
		return maps;
	}

	/**
	 * 全区灾情统计五个月
	 * @return
	 */
	@Override
	public List<Map> selectfivemonth() {
		List<Map> maps = sysEarthquakeMapper.selfivemonth();
		return maps;
	}

	/**
	 * 震源50km影响
	 * @param id
	 * @return
	 */
	@Override
	public List<Map> selectinfluence(int id) {
		List<Map> maps = sysEarthquakeMapper.selinfluence(id);
		return maps;
	}

	/**
	 * 最近一周的地震（4-8）级
	 * @return
	 */
	@Override
	public List<Map> selectfivedays() {
		List<Map> maps = sysEarthquakeMapper.selfivedays();
		return maps;
	}


}